package com.example.mapper;

import com.example.DTO.UserReadingStatDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface StatMapper {

//    获取某一阅读分类的阅读时长
    @Select("select sum(duration) from user_reading_stat where category_id = #{categoryId} and user_id = #{userid}")
    Integer getDuration(Integer userid , Integer categoryId);

    @Update("update user_reading_stat set duration = #{duration},updated_at = #{now} where user_id = #{userid} and category_id = #{categoryId}")
    void updateDuration(Integer userid, Integer categoryId, Integer duration, LocalDateTime now);

    @Insert("INSERT INTO user_reading_stat (user_id, category_id, duration, updated_at) " +
            "VALUES (#{userid}, #{categoryId}, #{duration}, #{now})")
    void addDuration(Integer userid, Integer categoryId, Integer duration, LocalDateTime now);

    @Select("select urs.user_id,c.id as category_id,c.category_name,urs.duration from user_reading_stat urs join category c on urs.category_id = c.id " +
            "where urs.user_id = #{userid}")
    List<UserReadingStatDTO> getUserReadingStat(Integer userid);
}
